package com.gl.rent;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.gl.common.util.SecurityUtils;
import com.haixiaoke.saas.repository.core.AjaxResult;
import com.haixiaoke.saas.repository.core.BaseController;
import com.haixiaoke.saas.repository.core.page.TableDataInfo;
import com.haixiaoke.saas.repository.pojo.HouseFacilities;
import com.haixiaoke.saas.repository.service.IHouseFacilitiesService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.MethodParameter;
import org.springframework.validation.BindException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 房屋设施Controller
 *
 * @author qixi
 * @date 2023-04-06
 */
@RestController
@RequestMapping("/house/facilities")
public class HouseFacilitiesController extends BaseController {

    @Autowired
    private  IHouseFacilitiesService houseFacilitiesService;

    /**
     * 查询房屋设施列表
     */
    @GetMapping("/list")
    public TableDataInfo list(HouseFacilities houseFacilities) {
        startPage();
        List<HouseFacilities> list = houseFacilitiesService.selectHouseFacilitiesList(houseFacilities);
        return getDataTable(list);
    }


    /**
     * 获取房屋设施详细信息
     */
    @GetMapping(value = "/{facilitiesId}")
    public AjaxResult getInfo(@PathVariable("facilitiesId") Long facilitiesId) {
        return success(houseFacilitiesService.selectHouseFacilitiesByFacilitiesId(facilitiesId));
    }

    /**
     * 新增房屋设施
     */
    @PostMapping
    public AjaxResult add(@Valid @RequestBody HouseFacilities houseFacilities) throws Exception{
        String id = Convert.toStr(SecurityUtils.getLoginUserId());
        houseFacilities.setCreateBy(id);

        //校验名称唯一
        String facilitiesName = houseFacilities.getFacilitiesName();
        if (!houseFacilitiesService.checkFacilitiesNameUnique(facilitiesName)) {
          return  error("设施名称已存在");
        }
        return toAjax(houseFacilitiesService.insertHouseFacilities(houseFacilities));
    }

    /**
     * 修改房屋设施
     */
//    @PreAuthorize("@ss.hasPermi('saas-repository:facilities:edit')")
    @PutMapping
    public AjaxResult edit(@Valid @RequestBody HouseFacilities houseFacilities) {
        String id = Convert.toStr(SecurityUtils.getLoginUserId());
        houseFacilities.setUpdateBy(id);

        HouseFacilities original = houseFacilitiesService.selectHouseFacilitiesByFacilitiesId(houseFacilities.getFacilitiesId());
        if (!StrUtil.equals(houseFacilities.getFacilitiesName(),original.getFacilitiesName())){
            //校验名称唯一
            String facilitiesName = houseFacilities.getFacilitiesName();
            if (!houseFacilitiesService.checkFacilitiesNameUnique(facilitiesName)) {
                return  error("设施名称已存在");
            }
        }
        return toAjax(houseFacilitiesService.updateHouseFacilities(houseFacilities));
    }

    /**
     * 删除房屋设施
     */
//    @PreAuthorize("@ss.hasPermi('saas-repository:facilities:remove')")
    @DeleteMapping("/{facilitiesIds}")
    public AjaxResult remove(@PathVariable Long[] facilitiesIds) {
        return toAjax(houseFacilitiesService.deleteHouseFacilitiesByFacilitiesIds(facilitiesIds));
    }
}
